<?php
/*
* 可识别txt类型的文件，如果文本文件的内容为Ansi则返回EUC-CN，如果内容为utf-8则返回UTF-8
* 能识别的编码有： UTF-8,UTF-16BE,UTF-16LE,UTF-32BE,UTF-32LE,EUC-CN(ANSI)
*/
define ('UTF32_BIG_ENDIAN_BOM'   , chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
define ('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
define ('UTF16_BIG_ENDIAN_BOM'   , chr(0xFE) . chr(0xFF));
define ('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
define ('UTF8_BOM'               , chr(0xEF) . chr(0xBB) . chr(0xBF));
class Encode_Core {
/**
* 文件分析方法来检查UNICODE文件，ANSI文件没有文件头，此处不分析
*/
  private function detect_utf_encoding($text) {
    $first2 = substr($text, 0, 2);
    $first3 = substr($text, 0, 3);
    $first4 = substr($text, 0, 3);
    if ($first3 == UTF8_BOM) return 'UTF-8';
    elseif ($first4 == UTF32_BIG_ENDIAN_BOM) return 'UTF-32BE';
    elseif ($first4 == UTF32_LITTLE_ENDIAN_BOM) return 'UTF-32LE';
    elseif ($first2 == UTF16_BIG_ENDIAN_BOM) return 'UTF-16BE';
    elseif ($first2 == UTF16_LITTLE_ENDIAN_BOM) return 'UTF-16LE';
  }
/**
* 取得编码
* @param string $str
* @return string $encoding
*/
  public static function get_encoding($str){
    $ary = array();
    $ary[] = 'UTF-8';
    $ary[] = 'ASCII';
    $ary[] = 'EUC-CN';
    $ary[] = 'JIS';//日文编码
    $ary[] = 'GB2312';//简体码
    $ary[] = 'BIG5';//繁体码
    $encoding=mb_detect_encoding($str, $ary);
    if(empty($encoding)){
      $encoding= self::detect_utf_encoding($str);
    }
    return $encoding;
  }
}



if (isset($_GET['dir'])){
	$basedir = $_GET['dir'];
}else{
	$basedir = '.';
}
$auto = 1;
//checkdir($basedir);

checkd($basedir);
function checkd($basedir){
	if ($dh = opendir($basedir)) {
		while (($file = readdir($dh)) !== false) {
		if ($file != '.' && $file != '..' && $file!='.svn'){
			if (!is_dir($basedir."/".$file)) {
				$ex = fileExtName($basedir."/".$file);
				if(!in_array($ex,array('php','html','js'))) continue;
				$c = checkencode("$basedir/$file");
				if($c != 'UTF-8'){
				echo "filename: $basedir/$file :".$c." <br>";
				}
				/*
				if($c != 'UTF-8'){echo "<b>";}
				echo "filename: $basedir/$file :".$c." <br>";
				if($c != 'UTF-8'){echo "</b>";}
				*/
			}else{
				$dirname = $basedir."/".$file;
				checkd($dirname);
				}
			}
			}
		closedir($dh);
	}
}


function checkencode($filename){
	$detect = new Encode_Core();
	$lrc = file_get_contents( $filename );
	$encode = $detect->get_encoding($lrc);
	return $encode;
}

function fileExtName ($fStr) {
   $retval = "";
   $pt = strrpos($fStr, ".");
   if ($pt) $retval = substr($fStr, $pt+1, strlen($fStr) - $pt);    return ($retval);
}

function checkdir($basedir){
	if ($dh = opendir($basedir)) {
		while (($file = readdir($dh)) !== false) {
		if ($file != '.' && $file != '..' && $file!='.svn'){
			if (!is_dir($basedir."/".$file)) {
				echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
			}else{
				$dirname = $basedir."/".$file;
				checkdir($dirname);
				}
			}
			}
		closedir($dh);
	}
}
function checkBOM ($filename) {
	global $auto;
	$contents = file_get_contents($filename);
	$charset[1] = substr($contents, 0, 1);
	$charset[2] = substr($contents, 1, 1);
	$charset[3] = substr($contents, 2, 1);
	if (ord($charset[1]) == 239 && ord($charset[2]) == 187 &&
ord($charset[3]) == 191) {
		if ($auto == 1) {
			$rest = substr($contents, 3);
			rewrite ($filename, $rest);
			return ("<font color=red>BOM found,automatically removed.</font>");
		} else {
			return ("<font color=red>BOM found.</font>");
		}
	}
	else return ("BOM Not Found.");
}
function rewrite ($filename, $data) {
	$filenum = fopen($filename, "w");
	flock($filenum, LOCK_EX);
	fwrite($filenum, $data);
	fclose($filenum);
}
?>